package Server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import standard.Standard;
import recordItem.Record;

public class ThreadReturn extends Thread{
	private Socket socket;
	private Standard standard;
	
	public ThreadReturn(Socket socket,Standard standard)
	{
		this.socket=socket;
		this.standard=standard;
	}
	
	public static int Search_read(Standard standard,Socket socket)
	{
		 for(String pattern:ThreadReader.data.keySet())
			{
				  if(pattern.equals(standard.getName()))
				  {
					  try{
						  OutputStream out=socket.getOutputStream();
						  out.write(ThreadReader.data.get(pattern).getData());
						  return 1;
					  }catch(IOException e)
					  {
						  e.printStackTrace();
					  }
				  }
			}
		 return 0;
	}
	
	public static int Search_in(Standard standard, Socket socket)
	{
		for(String pattern:ThreadReader.data.keySet())
		{
			if(pattern.equals(standard.getName()))
			{
				try{
					OutputStream out=socket.getOutputStream();
					out.write(ThreadReader.data.get(pattern).getData());
					ThreadReader.data.remove(pattern,ThreadReader.data.get(pattern));
					return 1;
				}catch(IOException e){
					e.printStackTrace();
				}			
			}
		}
		return 0;
	}
	
	public int Search_inAsyn(Standard standard, Socket socket)
	{
		boolean exist_or_not = false;
		for(String pattern:ThreadReader.data.keySet())
		{
			if(pattern.equals(standard.getName()))
			{
				exist_or_not = true;
				try{
					OutputStream out=socket.getOutputStream();
					ThreadReader.data.get(pattern).setAnswer("Exist");
					out.write(ThreadReader.data.get(pattern).getData());
					ThreadReader.data.remove(pattern,ThreadReader.data.get(pattern));
					return 1;
				}catch(IOException e){
					e.printStackTrace();
				}			
			}
		}
		if(exist_or_not == false)
		{
			Standard asyn_return_standard = new Standard();
			try {
				ByteArrayOutputStream ByteForObject = new ByteArrayOutputStream();
				ObjectOutputStream outStream = new ObjectOutputStream(ByteForObject);
				outStream.writeObject(asyn_return_standard);
				byte[] data = ByteForObject.toByteArray();
				
				OutputStream out = socket.getOutputStream();
				out.write(data);
			}catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	public int Search_readAsyn(Standard standard, Socket socket)
	{
		boolean exist_or_not = false;
		for(String pattern:ThreadReader.data.keySet())
		{
			if(pattern.equals(standard.getName()))
			{
				exist_or_not = true;
				try{
					OutputStream out=socket.getOutputStream();
					ThreadReader.data.get(pattern).setAnswer("Exist");
					out.write(ThreadReader.data.get(pattern).getData());
					return 1;
				}catch(IOException e){
					e.printStackTrace();
				}			
			}
		}
		if(exist_or_not == false)
		{
			Standard asyn_return_standard = new Standard();
			try {
				ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
				oos.writeObject(asyn_return_standard);
				return 0;
			}catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	@Override
	public void run()
	{
		if(standard.getMode().equals("in"))
		{
			int b;
			while(true)
			{
				b = ThreadReturn.Search_in(standard, socket);
				if(b==1)
				{
					break;
				}
			}
		}
		if(standard.getMode().equals("read"))
		{
			int b;
			while(true)
			{
				b = ThreadReturn.Search_read(standard, socket);
				if(b==1)
				{
					break;
				}
			}
		}
		if(standard.getMode().equals("readAsyn"))
	    {
            int b;
            b = Search_readAsyn(standard,socket);
            if(b==1)
            {
            	System.out.println("successfully return the object(read mode)");
            }
            else
            {
            	Record record = new Record(socket,standard);
            	ThreadReader.record_array.add(record);
            }
	    }
		if(standard.getMode().equals("inAsyn"))
		{
			int a;
			a = Search_inAsyn(standard,socket);
			if(a==1)
			{
				System.out.println("successfully return the object(inAsyn mode)");
			}
			else
			{
            	Record record = new Record(socket,standard);
            	ThreadReader.record_array.add(record);
			}
		}
      }
}
